import { DressUpData } from "../data/DressUpData";
import { ItemLogicBase } from "../../logicItem/ItemLogicBase";
import { GameTool } from "../../tool/GameTool";
import { GlobalObj, LableCfg } from "../../obj/GlobalObj";
import { PGCServer } from "../../PGCServer";
import { DressUpServerDataMgr, DressUpServerTag } from "../data/DressUpServerDataMgr";
import { GoodsType } from "../../uiComp/LevelListComp";
import { DressUpDataParse } from "../DressUpDataParse";

/**
 * 装扮部件item
 */
export class DressUpItemLogic extends ItemLogicBase {


    private selectBg: Laya.Image;
    private cricleMask: Laya.Image;
    private icon: Laya.Image;
    private nameLab: Laya.Label;
    private countLab: Laya.Label;

    private ownLab: Laya.Label;
    private priceLabBoxs: Laya.Box[] = [];
    private colorBox: Laya.Image;
    private originNameLabBottom: number;

    private _isWuGuan: boolean;
    private _ownLabName: string = "ownDDLab";

    constructor(box: Laya.Sprite) {
        super(box);
    }

    initNode(box: Laya.Box): void {
        this.selectBg = box.getChildByName("selectBg") as Laya.Image;
        this.cricleMask = box.getChildByName("cricleMask") as Laya.Image;
        this.icon = box.getChildByName("icon") as Laya.Image;
        this.nameLab = box.getChildByName("nameLab") as Laya.Label;
        this.countLab = box.getChildByName("countLab") as Laya.Label;
        this.nameLab.height = NaN;
        this.originNameLabBottom = this.nameLab.bottom;
        this.nameLab.overflow = "hidden";
    }

    initUi(data: any[]) {
        this.data = data;

        let goodsData: DressUpData = this.data[0];
        let needShowPrice = this.data[1];

        let isClear = goodsData && goodsData.preform_id < 0;
        this.initClearItem(isClear);
        GameTool.setSkin(this.getSelected() ? "pageres/roleCenterpage/xuanz(2).png" : "pageres/roleCenterpage/put.png", this.selectBg)

        if (isClear) return;

        let isNewPlayer = (GlobalObj.dressState == 2 && goodsData.new_user_free == 1);
        let needNewPlayerShop = (GlobalObj.dressState == 2 && !goodsData.new_user_free);
        needShowPrice = needShowPrice && !isNewPlayer;


        let nameDown = this.data[2];
        this._isWuGuan = this.data[3];
        DressUpServerDataMgr.Ins.getDressUpJson(goodsData, Laya.Handler.create(this, this.setDressUpUI))

        this.nameLab.text = goodsData.preform_name || goodsData.goods_name;

        if (nameDown && !needShowPrice) {
            this.nameLab.bottom = 16;
        } else {
            this.nameLab.bottom = this.originNameLabBottom;
        }

        if (this.ownLab) {
            this.ownLab.visible = false;
        }

        if (this.isShowPrice(goodsData) && needShowPrice) {
            if (GlobalObj.dressState !== 2) { // 新手引导不需要闯将价钱Lab---
                this._createPriceLab();
            }
            if (needNewPlayerShop) {
                if (this.priceLabBoxs[0]) {
                    this.priceLabBoxs[0].visible = false;
                }
                if (!this.ownLab) {
                    this.ownLab = this.box.getChildByName(this._ownLabName) as Laya.Label;
                    if (!this.ownLab) {
                        this.ownLab = Laya.Pool.getItemByClass("pgc_shop_tip_lab", Laya.Label);
                        this.ownLab.centerX = 0;
                        this.ownLab.bottom = 14;
                        this.ownLab.fontSize = 16;
                        this.ownLab.color = "#000000";
                        this.ownLab.name = this._ownLabName;
                        this.box.addChild(this.ownLab);
                    }
                }
                this.ownLab.text = "未拥有"
                this.ownLab.visible = true;
            }
        } else {
            if (this.priceLabBoxs[0]) {
                this.priceLabBoxs[0].visible = false;
            }
        }


        if (this.countLab) {
            let count = GlobalObj.getGoodsNum(goodsData);
            this.countLab.visible = count > 0 && needShowPrice;
            this.countLab.text = count + "";
            this.countLab.zOrder = 2;
        }


    }

    /** 初始化清理按钮 */
    private initClearItem(isClearItem: boolean) {
        if (this.priceLabBoxs[0]) {
            this.priceLabBoxs[0].visible = !isClearItem;
        }
        if (this.ownLab) {
            this.ownLab.visible = !isClearItem;
        }
        if (this.countLab) {
            this.countLab.visible = !isClearItem;
        }
        this.icon.visible = isClearItem;
        this.colorBox && (this.colorBox.visible = !isClearItem);
        this.cricleMask && (this.cricleMask.visible = !isClearItem);
        if (isClearItem) {
            GameTool.setSkin("pageres/bag/no_use.png", this.icon, false);
            this.nameLab.text = "不使用";
            this.icon.top = 20;
            this.icon.size(80, 80);
        }

        this.nameLab.alpha = isClearItem ? 0.6 : 1;
    }

    private _createPriceLab() {
        let goodsData: DressUpData = this.data[0];

        if (this.priceLabBoxs[0]) {
            this.priceLabBoxs[0].visible = true;
            let lab1 = (<Laya.Label>this.priceLabBoxs[0].getChildAt(0));
            let lab2 = (<Laya.Label>this.priceLabBoxs[0].getChildAt(1));
            lab1.text = goodsData.preform_price + "";
            lab2.x = lab1.width;
        } else {

            let box = GameTool.createContentLab(0, {
                labelCfgs: [
                    new LableCfg(goodsData.preform_price + "", 16, "#C8102E"),
                    new LableCfg("水晶", 16),
                ]
            }, this.priceLabBoxs);
            this.box.addChild(box);
            box.centerX = 0;
            box.bottom = 14;
        }
    }

    private setDressUpUI(goodsData: DressUpData) {
        if (!this.icon || this.icon.destroyed) return;
        if (goodsData.color && goodsData.color.length) {
            this.icon.visible = false;
            this.colorBox && (this.colorBox.visible = true);

            this.colorBox = GameTool.createCricleColor(80, GameTool.getRGBStr(goodsData.color)[0], this.colorBox);
            this.box.addChild(this.colorBox);
            this.colorBox.centerX = 0;
            this.colorBox.top = 20;
            if (this.cricleMask)
                this.cricleMask.visible = true;
        } else {
            this.icon.visible = true;
            if (this.cricleMask)
                this.cricleMask.visible = false;
            if (this.colorBox) {
                this.colorBox.visible = false;
            }
            let url = goodsData.iconUrl || goodsData.goods_icon_url || DressUpDataParse.getPlatformImgUrl(goodsData);
            if (url.indexOf(".") >= 0) {
                GameTool.setSkin(url, this.icon, true);
            } else {
                this.icon.skin = "";
            }
            let isxingzhuang = DressUpServerDataMgr.Ins.containTagByKey("形状_" + DressUpServerTag.ZHUANG_RONG_TAG, goodsData.system_tag_ids);
            let isdabaibing = DressUpServerDataMgr.Ins.containTagByKey(GoodsType.Z_DING_ZHUANG+"_" + DressUpServerTag.ZHUANG_RONG_TAG, goodsData.system_tag_ids);
            let isbone = goodsData.preform_type == DressUpServerTag.AVATAR_BONE_TAG;
            if (this._isWuGuan || isxingzhuang || isdabaibing) {
                this.icon.top = 6;
                this.icon.size(116, 116);
            } else {

                this.icon.top = 22;
                this.icon.size(75, 110);
            }
            if (isbone) {
                this.icon.top = 6;
                this.icon.size(100, 100);
            }
        }
    }

    onClick() {
        console.log("点击")
    }

    private isShowPrice(goodsData: DressUpData) {
        return goodsData && !GlobalObj.getGoodsNum(goodsData) && !goodsData.developer_avatar_json && !goodsData.define_id;
    }


    protected setSelected(selected: boolean): void {
        GameTool.setSkin(selected ? "pageres/roleCenterpage/xuanz(2).png" : "pageres/roleCenterpage/put.png", this.selectBg)
    }

    clear(): void {
        GameTool.recoverBoxInLab(this.priceLabBoxs);
        GameTool.recoverTipLab(this.ownLab);
        this.nameLab.bottom = this.originNameLabBottom;
        if (this.colorBox) {
            this.colorBox.destroy();
            this.colorBox = null;
        }
        this.priceLabBoxs.length = 0;
        this.icon = null;
        this.selectBg = null;
        this.nameLab = null;
        this.box = null;
        this.data = null;
    }
}